package com.sky.controller.admin;


import com.sky.constant.MessageConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.MealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RequestMapping("/admin/setmeal")
@RestController
@Slf4j
@Api(tags = "套餐管理相关接口")
public class MealController {
    @Autowired
    private MealService mealService;
//    @Autowired
//    private SetmealMapper setmealMapper;




    /**
     * 新增套餐
     */
    @PostMapping
    @ApiOperation("新增套餐操作")
    @CachePut(cacheNames = "setmealCache",key = "#setmealDTO.categoryId")//setmealCache::100
    public Result addMeal(@RequestBody SetmealDTO setmealDTO){
        mealService.insert(setmealDTO);
        return Result.success();
    }

    /**
     * 分页查询套餐
     */
    @GetMapping("/page")
    @ApiOperation("分页查询操作")
    public Result<PageResult>  pagemeal(SetmealPageQueryDTO setmealPageQueryDTO){
        log.info("分页查询{}",setmealPageQueryDTO);
        PageResult pageResult=mealService.page(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 套餐的起售与停售
     */
    @PostMapping("/status/{status}")
    @ApiOperation("套餐的起售与停售")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result startOrStopMeal(@PathVariable("status") Long status,Integer id){
        log.info("更新套餐状态:{}",status);
        mealService.startOrStop(status,id);
        return Result.success();
    }

    /**
     * 修改套餐
     */
    @PutMapping
    @ApiOperation("修改套餐")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result updateMeal(@RequestBody SetmealDTO setmealDTO){
        mealService.updateMeal(setmealDTO);
        return Result.success();
    }

    /**
     * 根据id查询套餐
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询套餐")
    @Cacheable(cacheNames = "setmealCache",key = "#id")
    public Result<SetmealVO> getById(@PathVariable  Long id){
        SetmealVO setmealVO=mealService.getById(id);
        return Result.success(setmealVO);
    }

    /**
     * 批量删除套餐
     */

    @ApiOperation("批量删除套餐")
    @DeleteMapping
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result delete(@RequestParam List<Long> ids){
        //在售套餐不可以删除
        for (Long id : ids) {
            Integer status=mealService.getStatus(id);
            log.info("。。。。。。。。。。。。{}",status);
            if(status.equals(1)){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
            mealService.deleteById(id);
        }
        return Result.success();
    }
}
